package com.jty.lib_base.utils

import android.content.Context
import com.zhihu.matisse.MimeType
import com.zhihu.matisse.filter.Filter
import com.zhihu.matisse.internal.entity.IncapableCause
import com.zhihu.matisse.internal.entity.Item
import com.zhihu.matisse.internal.utils.PhotoMetadataUtils


/**
 * author: zhouyh
 * created on: 2022/4/9 3:13 下午
 * description: 过滤选择图片大小
 */
class ImageSizeFilter(var minWidth: Int, var minHeight: Int,private var maxSize: Int ) : Filter() {

    override fun constraintTypes(): Set<MimeType?>? {
        return object : HashSet<MimeType?>() {
            init {
                add(MimeType.GIF)
            }
        }
    }

    override fun filter(context: Context?, item: Item?): IncapableCause? {
        if (needFiltering(context,item))
            return null

        val size = PhotoMetadataUtils.getBitmapBound(context!!.contentResolver, item!!.contentUri)
        return if (size.x < minWidth || size.y < minHeight) {
            IncapableCause(IncapableCause.DIALOG,
                "图片太小，请重新选择")
        } else if(item!!.size > maxSize){
            IncapableCause(IncapableCause.DIALOG,
                "图片最大不能超过10M")
        } else null
    }
}